package com.hyperfix.simulator.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ThreadPoolExecutor;

import static com.hyperfix.models.constant.system.SystemConstant.POLLER_THREAD_COUNT;

/**
 * @author JiangChunYu
 * 2024/3/12
 */
@Configuration
public class SchedulingConfig {
    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(POLLER_THREAD_COUNT * 8);
        taskScheduler.setThreadNamePrefix("simulator-");
        taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        taskScheduler.setRemoveOnCancelPolicy(true);
        taskScheduler.setAwaitTerminationSeconds(5);
        taskScheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        taskScheduler.initialize();
        return taskScheduler;
    }
}
